# common functions

def getCycleNumber (i_solve, total_cycle):
		i_cycle    = i_solve%total_cycle 
		if i_cycle == 0:
			i_cycle = total_cycle
		return i_cycle

def getTimeStep (i_solve, total_cycle):
		i_timeStep = (i_solve-1)/total_cycle + 1 

		return i_timeStep


def findObjLineRange(total_block_to_search,label_begin,label_end,begin_offset,end_offset,infile_path):
## find obj line range

	print "total_block_to_search: "+str(total_block_to_search)
	i_start= []
	i_end = []
	infile = open(infile_path,"r")
	
	in_block_obj = False #false

	for i, line in enumerate(infile.readlines()):

		if ( (in_block_obj == False) and (label_begin in line) ):
			i_start.append(i+begin_offset)
			in_block_obj = True
			
		if ( (in_block_obj == True) and (label_end in line) ):	
			i_end.append(i+end_offset)
			in_block_obj = False
		
		if len(i_end)== total_block_to_search:
			break	
		
	# check if end_of_file encountered before setting i_end		
	if ( len(i_start) == len(i_end)+1 ):
		i_end.append(i)
	elif ( len(i_start) > len(i_end)+1 ):
		print "Error in findObjLineRange()"
		
	print "i_start: "
	print i_start
	print "i_end"
	print i_end
	
	infile.close()
	
	outPosDict = {}
	
	for i, (pos_start, pos_end) in enumerate(zip(i_start,i_end)):
		
		outPosDict[i]= {"start":pos_start, "end":pos_end}
		
	#print outPosDict			
	return outPosDict	